﻿using SpeechLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockHelper.Scripts.Comm
{
    public class Voice
    {

        private static SpVoice voice = new SpVoice();


        public static void Init() {

            //voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);


            Console.WriteLine(string.Join(", ", getDescription()));


        }

        /// <summary>
        /// 获取语音库
        /// </summary>
        /// <returns>List<string></returns>
        public static List<string> getDescription()
        {
            List<string> list = new List<string>();
            ISpeechObjectTokens obj = voice.GetVoices();
            int count = obj.Count;//获取语音库总数
            for (int i = 0; i < count; i++)
            {
                string desc = obj.Item(i).GetDescription(); //遍历语音库
                list.Add(desc);
            }
            return list;
        }

        /// <summary>
        /// 设置当前使用语音库
        /// </summary>
        /// <returns>bool</returns>
        public static bool setDescription(string name)
        {
            List<string> list = new List<string>();
            ISpeechObjectTokens obj = voice.GetVoices();
            int count = obj.Count;//获取语音库总数
            bool result = false;
            for (int i = 0; i < count; i++)
            {
                string desc = obj.Item(i).GetDescription(); //遍历语音库
                if (desc.Equals(name))
                {
                    voice.Voice = obj.Item(i);
                    result = true;
                }
            }
            return result;
        }

        /// <summary>
        /// 设置语速
        /// </summary>
        /// <param name="n"></param>
        public static void setRate(int n)
        {
            voice.Rate = n;
        }

        /// <summary>
        /// 设置声音大小
        /// </summary>
        /// <param name="n"></param>
        public static void setVolume(int n)
        {
            voice.Volume = n;
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public static void Pause()
        {
            voice.Pause();
        }

        /// <summary>
        /// 继续
        /// </summary>
        public static void Resume()
        {
            voice.Resume();
        }

        /// <summary>
        /// 停止
        /// </summary>
        public static void Stop()
        {
            voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
        }

        public static void Speak(string msg,bool now = false) {
            if (now) Stop();
            voice.Speak(msg, now ? SpeechVoiceSpeakFlags.SVSFDefault : SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }

        

    }
}
